package com.qinker.oa.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qinker.oa.dao.CustomerInfoDao;
import com.qinker.oa.exception.GlobalException;
import com.qinker.oa.message.CodeMsg;
import com.qinker.oa.model.CustomerContactMessage;
import com.qinker.oa.model.CustomerInfo;
import com.qinker.oa.model.User;
import com.qinker.oa.model.vo.CustomerInfoVo;
import com.qinker.oa.service.CustomerInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class CustomerInfoServiceImpl implements CustomerInfoService {

    @Autowired
    private CustomerInfoDao customerInfoDao;

    @Override
    public PageInfo<CustomerInfoVo> getCustomerInfoListAll(int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        List<CustomerInfoVo> infoList = customerInfoDao.getCustomerInfoListAll();
        PageInfo<CustomerInfoVo> pageInfo = new PageInfo<>(infoList);
        return pageInfo;
    }

    @Override
    public List<CustomerInfoVo> getCustomerInfoListAllWithNoPage() {
        List<CustomerInfoVo> infoList = customerInfoDao.getCustomerInfoListAll();
        return infoList;
    }

    @Override
    public PageInfo<CustomerInfoVo> getCustomerInfoListPerson(int page, int pageSize, int uid) {

        PageHelper.startPage(page,pageSize);
        List<CustomerInfoVo> infoList = customerInfoDao.getCustomerInfoListPerson(uid);
        PageInfo<CustomerInfoVo> pageInfo = new PageInfo<>(infoList);
        return pageInfo;
    }

    @Override
    public List<CustomerInfoVo> getCustomerInfoListPersonWithNoPage(int uid) {
        List<CustomerInfoVo> infoList = customerInfoDao.getCustomerInfoListPerson(uid);
        return infoList;
    }

    @Override
    public void addCustomerInfo(CustomerInfo info, HttpSession session) {
        User user = (User) session.getAttribute("user");
        Timestamp createTime = Timestamp.valueOf(LocalDateTime.now());
        info.setCreateTime(createTime);
        info.setUId(user.getId());
        customerInfoDao.addCustomer(info);
    }

    @Override
    public PageInfo<CustomerInfoVo> search(int page, int pageSize,String customerNumber, String customerName, int[] ids) {
        if (customerNumber == null || customerNumber.equals("")){
            customerNumber = null;
        }else {
            customerNumber = "%" + customerNumber + "%";
        }
        if (customerName == null || customerName.equals("")){
            customerName = null;
        }else {
            customerName = "%" + customerName + "%";
        }
        PageHelper.startPage(page,pageSize);
        List<CustomerInfoVo> infoVos = customerInfoDao.search(customerNumber, customerName, ids);
        PageInfo<CustomerInfoVo> pageInfo = new PageInfo<>(infoVos);
        return pageInfo;
    }

    @Override
    public List<CustomerInfoVo> downloadExcelAllByUser(String customerNumber, String customerName, int[] ids) {
        if (customerNumber == null || customerNumber.equals("")){
            customerNumber = null;
        }else {
            customerNumber = "%" + customerNumber + "%";
        }
        if (customerName == null || customerName.equals("")){
            customerName = null;
        }else {
            customerName = "%" + customerName + "%";
        }
        List<CustomerInfoVo> infoVos = customerInfoDao.search(customerNumber, customerName, ids);
        return infoVos;
    }

    @Override
    @Transactional
    public void updateInfoById(CustomerInfo info) {
        customerInfoDao.updateInfoById(info);
    }

    @Override
    public List<CustomerContactMessage> getMessageById(int aId) {
        return customerInfoDao.getMessageById(aId);
    }

    @Override
    public void insertSchedule(CustomerContactMessage message) {
        try{
            customerInfoDao.insertSchedule(message);
        }catch (Exception e){
            throw new GlobalException(CodeMsg.SERVER_ERROR);
        }
    }

    @Override
    @Transactional
    public void deleteCustomer(int id) {
        try {
            customerInfoDao.deleteInfoMessage(id);
            customerInfoDao.deleteCustomerInfo(id);
        }catch (Exception e){
            throw new GlobalException(CodeMsg.SERVER_ERROR);
        }
    }
}
